/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.UsuarioDAO;
import Model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 0433101
 */
public class loginProcessor extends Processor {    
   
    @Override
    public void execute() throws ServletException, IOException {
        getResponse().setContentType("text/html;charset=UTF-8");
        PrintWriter out = getResponse().getWriter();
        try {
            String login = getRequest().getParameter("login");
            String senha = getRequest().getParameter("senha");
            RequestDispatcher rd;
            
            Usuario user = UsuarioDAO.pesquisarByLogin(login);
            
            if(user == null){
                String erro = "Login inválido!";
                getRequest().setAttribute("erro", erro);
                rd = getRequest().getRequestDispatcher("index.jsp");
                rd.forward(getRequest(), getResponse());
            } 
            else if(!user.getSenha().equals(senha)){
                String erro = "Login inválido!";
                getRequest().setAttribute("erro", erro);
                rd = getRequest().getRequestDispatcher("index.jsp");
                rd.forward(getRequest(), getResponse());
            }
            else if(user.isBloquear()){
                String erro = "Usuário bloqueado!";
                getRequest().setAttribute("erro", erro);
                rd = getRequest().getRequestDispatcher("index.jsp");
                rd.forward(getRequest(), getResponse());
            }
            else{
                String message = "Bem Vindo, " + user.getNome();                
                getRequest().setAttribute("mensagem", message);
                HttpSession session = getRequest().getSession(true);
                session.setAttribute("usuario", user);   
                int privilegio = user.getPrivilegio().getId();
                
                if(privilegio == 1){
                    rd = getRequest().getRequestDispatcher("menuAdministrador.jsp");
                    rd.forward(getRequest(), getResponse());
                }
                else if(privilegio == 2){
                    rd = getRequest().getRequestDispatcher("menuFuncionario.jsp");
                    rd.forward(getRequest(), getResponse());
                }
                else if(privilegio == 3){
                    rd = getRequest().getRequestDispatcher("menuPorteiro.jsp");
                    rd.forward(getRequest(), getResponse());
                }
                else if(privilegio == 4){
                    rd = getRequest().getRequestDispatcher("menuAluno.jsp");
                    rd.forward(getRequest(), getResponse());
                }
            }
        }
        catch(SQLException e){
            String erro = "Não é possível acessar o banco de dados!";
            getRequest().setAttribute("erro", erro);
            RequestDispatcher rd = getRequest().getRequestDispatcher("index.jsp");
            rd.forward(getRequest(), getResponse());
        }
        catch(ClassNotFoundException e){
            String erro = "Problema no sistema! Tente mais tarde!";
            getRequest().setAttribute("erro", erro);
            RequestDispatcher rd = getRequest().getRequestDispatcher("index.jsp");
            rd.forward(getRequest(), getResponse());
        }
    }
}
